package org.jxc.web.action;

import java.util.List;

import org.jxc.constants.JXCString;
import org.jxc.domain.Supplier;
import org.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.team4.domain.Dictionary;
import org.team4.service.DictionaryService;
import org.team4.web.action.GenericAction;

@Controller
@Scope("prototype")
public class SupplierAction extends GenericAction<Supplier> {

	private static final long serialVersionUID = 3702197771690053939L;

	@Autowired
	private SupplierService supplierService;
	@Autowired
	private DictionaryService dictionaryService;

	private List<Dictionary> typeList;
	private List<Dictionary> regionList;
	private List<Dictionary> payTypeList;
	private Supplier supplier;
	private String supplierId;

	public String listPagination() throws Exception {
		page = supplierService.listPagionation(page);
		return SUCCESS;
	}

	public String toSave() throws Exception {
		typeList = dictionaryService.findDictionaryBySuperId(JXCString.S_TYPE);
		regionList = dictionaryService
				.findDictionaryBySuperId(JXCString.REGION);
		payTypeList = dictionaryService
				.findDictionaryBySuperId(JXCString.PAY_TYPE);
		return SUCCESS;
	}

	public String save() throws Exception {
		supplierService.saveSupplier(supplier);
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		typeList = dictionaryService.findDictionaryBySuperId(JXCString.S_TYPE);
		regionList = dictionaryService
				.findDictionaryBySuperId(JXCString.REGION);
		payTypeList = dictionaryService
				.findDictionaryBySuperId(JXCString.PAY_TYPE);
		supplier = supplierService.findById(supplierId);
		return SUCCESS;
	}

	public String update() throws Exception {
		supplierService.updateSupplier(supplier);
		return SUCCESS;
	}

	public String toView() throws Exception {
		supplier = supplierService.findById(supplierId);
		return SUCCESS;
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		for (String str : idList) {
			try {
				supplierService.deleteSupplier(str);
			} catch (Exception e) {
				continue;
			}
		}
		return SUCCESS;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public List<Dictionary> getTypeList() {
		return typeList;
	}

	public List<Dictionary> getRegionList() {
		return regionList;
	}

	public List<Dictionary> getPayTypeList() {
		return payTypeList;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}
}
